package com.highper.gateway.factory;

import com.highper.gateway.filter.GrayLoadBalancerClientFilter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.annotation.Order;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @Author ZhangGJ
 * @Date 2022/02/16 09:43
 */
@Slf4j
@Order(3)
public class GrayHeaderGatewayFilterFactory
		extends AbstractGatewayFilterFactory<GrayHeaderGatewayFilterFactory.Config> {

	@Autowired
	private GrayLoadBalancerClientFilter balancer;

	@Override
	public List<String> shortcutFieldOrder() {
		return null;
	}

	public GrayHeaderGatewayFilterFactory() {
		super(Config.class);
	}

	@Override
	public GatewayFilter apply(Config config) {
		return (exchange, chain) -> chain.filter(exchange)
				.then(Mono.fromRunnable(() -> balancer.choose(exchange)));
	}

	@Data
	public static class Config {
	}

}
